import Vue from "vue";
import Vuex from "vuex";
import storage from "@/utils/storage";
import {
	getCardRoomDetail
} from "@/api/game.js"
import socket from "socket.io-client/lib/socket";

Vue.use(Vuex);

let time1 = 0

const store = new Vuex.Store({
	state: {
		isShowToast: false, // 是否在展示Toast中
		remark: [], //填写订单备注
		shareLink: "", //分享链接
		distributionId: "", //分销员Id 如果当前账户从未登录过时记录
		hasLogin: storage.getHasLogin(),
		userInfo: storage.getUserInfo(),
		uuid: storage.getUuid(),
		token: "",
		vKeyboardHeight: 0,
		inviteInputHeight: 0,
		addThreadFlage: false, //添加线索动画显示布尔值
		lockedUrl: "",
		defaultCoverUrl: "",

		// 存放全局事件
		events: [],

		// 游戏上下文信息
		globalGameCtx: {},
		// 播放记录列表（自增ID
		playHistoryIds: [],

		loopIndex: 0, //当前循环列表播放下标
		curStoryId: null, //当前项的storyId
		curStoryType: null,
		testData: null,
		currentCardId: null, //当前卡牌id
		currentCardData: null,

		systemInfo: storage.getSystemInfo(),

		// 用户信息数据
		// userInfo:{
		// 	username: "",
		// 	headUrl: "",
		// 	mobile: "",
		// 	sex: "",
		// 	birthday: ""
		// }
		screenWidth: 375,
		menuButtonInfo: {
			top: 0,
			height: 0,
			left: 0
		},
		messageList: [],
		magicText: ""
	},
	getters: {
		magicText(state) {
			return state.magicText
		},
		systemInfo(state) {
			if (state.systemInfo) {
				return state.systemInfo
			} else {
				let systemInfo = uni.getSystemInfoSync()
				storage.setSystemInfo(systemInfo)
				return systemInfo
			}
		},
		testData(state) {
			return state.testData
		},
		curStoryId(state) {
			return state.curStoryId
		},
		curStoryType(state) {
			return state.curStoryType
		},
		globalGameCtx(state) {
			let date = new Date()
			console.log("监听到变化重新赋值", date - time1)
			return state.globalGameCtx
		},
		playHistoryIds(state) {
			return state.playHistoryIds
		},
		curGlobalStoryId(state) {
			return state.globalGameCtx.storyId
		},
		curGlobalLoopList(state) {
			return state.globalGameCtx.playStoryList
		},
		loopIndex(state) {
			return state.loopIndex
		},
		isNewInt(state) {
			return !state.globalGameCtx
		},
		currentCardId(state) {
			return state.currentCardId
		},
		currentCardData(state) {
			return state.currentCardData
		},
		userInfo(state) {
			return state.userInfo
		},
		screenWidth(state) {
			return state.screenWidth
		},
		menuButtonInfo(state) {
			return state.menuButtonInfo
		},
		messageList(state) {
			return state.messageList
		}

	},
	mutations: {
		updateMagicText(state, payload) {
			state.magicText = payload
		},
		updateMenuButtonInfo(state, payload) {
			console.log("发生更新", payload)
			state.menuButtonInfo = payload
		},
		updateScreenWidth(state, payload) {
			state.screenWidth = payload
		},
		updateCurrentCardId(state, payload) {
			state.currentCardId = payload
		},
		updateTestData(state, payload) {
			state.testData = payload
		},
		updateUerInfo(state, payload) {
			state.userInfo = payload
			storage.setUserInfo(payload)
		},

		updateCurStoryId(state, payload) {
			state.curStoryId = payload
		},
		updateCurStoryType(state, payload) {
			state.curStoryType = payload
		},
		setLoopIndex(state, index) {
			state.loopIndex = index
		},
		changeDefaultCoverUrl(state, payload) {
			state.defaultCoverUrl = payload
		},
		changeLockedUrl(state, payload) {
			state.lockedUrl = payload
		},
		changeAddThreadFlage(state, payload) {
			// console.log("发生修改", payload);
			state.addThreadFlage = payload
		},
		changeInviteInputHeight(state, payload) {
			state.inviteInputHeight = payload
		},
		changeKeyBordHeight(state, payload) {
			state.vKeyboardHeight = payload
		},
		login(state, userInfo) {
			state.userInfo = userInfo || {};
			state.userName =
				userInfo.Name || userInfo.Nickname || userInfo.Username || "匿名用户";
			state.hasLogin = true;
		},
		logout(state) {
			state.userName = "";
			state.hasLogin = false;
		},


		// 设置填写订单中备注
		setRemark(state, remark) {
			state.remark = remark;
		},

		// 注册全局事件
		regEvent(state, event) {
			state.events.push(event)
		},
		// 执行全局事件
		doEvent(state, params) {
			state.events.forEach(e => {
				// console.log('执行全局事件');
				e(params)
			})
		},
		// 注销事件
		removeEvent(state, event) {
			let index = state.events.findIndex(item => {
				return item === event
			})
			if (index !== -1) {
				state.events.splice(index, 1)
			}
		},

		// 更新游戏上下文信息
		updateGlobalGameCtx(state, globalGameCtx) {
			state.globalGameCtx = globalGameCtx

		},

		// 播放记录列表（自增ID
		addPlayHistoryIds(state, playHistoryId) {
			if (!state.playHistoryIds.includes(playHistoryId)) {
				state.playHistoryIds.push(playHistoryId)
			}
		},
		// 清空记录列表（自增ID
		clearPlayHistoryIds(state) {
			state.playHistoryIds = []
		},
		updateCurrentCardData(state, payload) {
			state.currentCardData = payload
		},
		updateMessageList(state, payload) {
			console.log("修改", payload)
			state.messageList = payload
		},
		pushMessage(state, payload) {
			state.messageList.push(payload)
		}
	},
	actions: {
		// 拉取消息
		updateMessageList({
			commit
		}, payload) {
			// 实现消息拉取和处理逻辑

			commit('updateMessageList', payload)
		},
		// 分发注册全局事件
		audioOn({
			commit
		}, event) {
			commit('regEvent', event)
		},
		// 分发执行全局事件
		audioEmit({
			commit
		}, params) {
			commit('doEvent', params)
		},
		// 分发注销全局事件
		audioOff({
			commit
		}, event) {
			commit('removeEvent', event)
		},

		// 更新游戏上下文信息
		updateGlobalGameCtx({
			commit
		}, globalGameCtx) {
			return new Promise((resolve, reject) => {
				commit('updateGlobalGameCtx', globalGameCtx)
				resolve(true)
			})
		},
		// 更新游戏上下文信息
		setLoopIndex({
			commit
		}, index) {
			return new Promise((resolve, reject) => {
				commit('setLoopIndex', index)
				resolve(true)
			})
		},

		// 播放记录列表（自增ID
		addPlayHistoryIds({
			commit
		}, playHistoryId) {
			return new Promise((resolve, reject) => {
				commit('addPlayHistoryIds', playHistoryId)
				resolve(true)
			})
		},
		// 播放记录列表（自增ID
		clearPlayHistoryIds({
			commit
		}) {
			return new Promise((resolve, reject) => {
				commit('clearPlayHistoryIds')
				resolve(true)
			})
		},
		// 获取最新卡牌数据
		getRoomCardDetail({
			commit
		}, params) {
			return new Promise((resolve, reject) => {
				getCardRoomDetail(params).then(res => {
					let result = res.data.result
					commit("updateCurrentCardData", result)
					resolve(true)
				}).catch((err) => {
					reject(false)
				})
			})

		}
	},
});

export default store;